package org.infodavid.common.model.impl;

import org.infodavid.common.model.IUser;
import org.infodavid.common.model.IUserGroup;
import org.infodavid.common.model.components.IOwnerDetails;

/**
 * The Class OwnerDetailsImpl.
 */
public class OwnerDetailsImpl extends AbstractBasicDataObjectImpl implements IOwnerDetails {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -6115081552259996658L;

	/** The owner user. */
	private IUser user;

	/** The owner group. */
	private IUserGroup group;

	/** The visibility. */
	private EVisibility visibility;

	/*
	 * Instantiates a new ownable impl.
	 * @throws Exception the exception
	 */
	/**
	 * Instantiates a new owner details impl.
	 * @throws Exception the exception
	 */
	protected OwnerDetailsImpl() throws Exception {
		super();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IOwnerDetails#getUser()
	 */
	@Override
	public IUser getUser() {
		return user;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IOwnerDetails#getGroup()
	 */
	@Override
	public IUserGroup getGroup() {
		return group;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IOwnerDetails#setUser(org.infodavid.common.model.IUser)
	 */
	@Override
	public void setUser(final IUser value) {
		if (!equals(user, value)) {
			user = value;
			markModified(USER);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IOwnerDetails#setGroup(org.infodavid.common.model.IGroup)
	 */
	@Override
	public void setGroup(final IUserGroup value) {
		if (!equals(group, value)) {
			group = value;
			markModified(GROUP);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IOwnerDetails#getVisibility()
	 */
	@Override
	public EVisibility getVisibility() {
		return visibility;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.IOwnerDetails#setVisibility(org.infodavid.common.model.IOwnerDetails
	 * .EVisibility)
	 */
	@Override
	public void setVisibility(final EVisibility value) {
		if (!equals(visibility, value)) {
			visibility = value;
			markModified(VISIBILITY);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.BasicDataObjectImpl#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		user = null;
		group = null;
		visibility = EVisibility.PUBLIC;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		final StringBuilder builder = new StringBuilder();

		builder.append(super.toString());
		builder.append(" [user=");

		if (user == null) {
			builder.append("null");
		}
		else {
			builder.append(user.getKey());
		}

		builder.append(", group=");

		if (group == null) {
			builder.append("null");
		}
		else {
			builder.append(group.getKey());
		}

		builder.append(", visibility=");
		builder.append(visibility);
		builder.append("]");

		return builder.toString();
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + (getGroup() == null ? 0 : getGroup().hashCode());
		result = prime * result + (getUser() == null ? 0 : getUser().hashCode());

		return prime * result + (getVisibility() == null ? 0 : getVisibility().hashCode());
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof IOwnerDetails)) {
			return false;
		}

		final IOwnerDetails other = (IOwnerDetails)obj;

		if (getGroup() == null) {
			if (other.getGroup() != null) {
				return false;
			}
		}
		else if (!getGroup().equals(other.getGroup())) {
			return false;
		}

		if (getUser() == null) {
			if (other.getUser() != null) {
				return false;
			}
		}
		else if (!getUser().equals(other.getUser())) {
			return false;
		}

		if (getVisibility() != other.getVisibility()) {
			return false;
		}

		return true;
	}
}
